package juc.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueTest {

    final static BlockingQueue<Integer> blockQueue=new SynchronousQueue<>(true);

    public static void main(String[] args) throws InterruptedException {

        //TODO 模拟消费者取数据
        new Thread(()->{take();},"consumer1").start();
        Thread.sleep(10);
        new Thread(()->{take();},"consumer2").start();
        Thread.sleep(100);

        //TODO 模拟生产者写数据
        new Thread(()->{put(1);},"producer1").start();
        Thread.sleep(10);
        new Thread(()->{put(5);},"producer2").start();

    }

    public static void take(){
        try {
            Integer item = blockQueue.take();
            System.out.println(Thread.currentThread().getName()+" Take:"+item);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void put(Integer item){
        try {
            System.out.println(Thread.currentThread().getName()+" Put:"+item);
            blockQueue.put(item);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

